<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      v-model="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataFormRef"
             @keyup.enter="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="来源名称" prop="name"
                    label-width="120px">
        <el-input v-model="dataForm.name" placeholder="来源名称"></el-input>
      </el-form-item>
      <el-form-item label="来源地址"
                    label-width="120px" prop="url">
        <el-input v-model="dataForm.url" placeholder="来源地址url"></el-input>
      </el-form-item>
      <el-form-item label="更新周期"
                    label-width="120px" prop="period">
        <el-input v-model="dataForm.period" placeholder="更新周期"></el-input>
      </el-form-item>

    </el-form>
    <template #footer>
        <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import {detail, post_data} from '@/apis/sources'
import useDialogForm from "../../hooks/useDialogForm";
import {ref} from "vue";

const refreshDataListEmitName = 'refreshDataList'
export default {
  name: 'source-add-or-update-component',
  emits: [refreshDataListEmitName],
  setup(prop, context) {

    const dataFormRef = ref(null)
    const {
      visible, dataForm,
      doAddOrUpdateHandle,
      doGetDetail,
      doDataFormSubmit, init,
    } = useDialogForm(
        {detailFun: detail, postDataFun: post_data},
        ['id', 'name', 'url', 'period'],
        dataFormRef
    )
    return {
      visible,
      dataForm,
      dataFormRef,
      init,
      doAddOrUpdateHandle,
      dataFormSubmit: () => {
        doDataFormSubmit().then(() => {
          context.emit(refreshDataListEmitName)
        })
      },
      getDetail: doGetDetail,
    }
  },
  data() {
    return {
      dataRule: {
        name: [
          {required: true, message: '来源名称不能为空', trigger: 'blur'}
        ],
        url: [
          {required: true, message: '来源地址不能为空', trigger: 'blur'}
        ],
        period: [
          {required: true, message: '爬取周期不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {}
}
</script>